<html>
<head><meta charset="utf-8"><title>Format string syntax #3419 · t-compiler/rust-analyzer · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/index.html">t-compiler/rust-analyzer</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Format.20string.20syntax.20.233419.html">Format string syntax #3419</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="213045274"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Format%20string%20syntax%20%233419/near/213045274" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lukas Wirth <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Format.20string.20syntax.20.233419.html#213045274">(Oct 12 2020 at 14:29)</a>:</h4>
<p>For <a href="https://github.com/rust-analyzer/rust-analyzer/issues/3419">https://github.com/rust-analyzer/rust-analyzer/issues/3419</a> where would the checks for this ideally go? I would imagine in the syntax crate in validation?</p>



<a name="213045329"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Format%20string%20syntax%20%233419/near/213045329" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Format.20string.20syntax.20.233419.html#213045329">(Oct 12 2020 at 14:29)</a>:</h4>
<p>Yup</p>



<a name="213045369"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Format%20string%20syntax%20%233419/near/213045369" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Format.20string.20syntax.20.233419.html#213045369">(Oct 12 2020 at 14:30)</a>:</h4>
<p>Specifically, the <code>syntax</code> crate should have a pretty abstract way to tokenize a string</p>



<a name="213045419"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Format%20string%20syntax%20%233419/near/213045419" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Format.20string.20syntax.20.233419.html#213045419">(Oct 12 2020 at 14:30)</a>:</h4>
<p>and then that should be used by other crates</p>



<a name="213045456"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Format%20string%20syntax%20%233419/near/213045456" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Format.20string.20syntax.20.233419.html#213045456">(Oct 12 2020 at 14:30)</a>:</h4>
<p>I <em>think</em> we already have something like this already, for syntax highlighting</p>



<a name="213045553"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Format%20string%20syntax%20%233419/near/213045553" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lukas Wirth <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Format.20string.20syntax.20.233419.html#213045553">(Oct 12 2020 at 14:31)</a>:</h4>
<p>Yep, there is a <code>HasFormatSpecifier</code> trait implemented on String and RawString which offers a lexing function for this thanks to syntax highlighting</p>



<a name="213045577"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Format%20string%20syntax%20%233419/near/213045577" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Format.20string.20syntax.20.233419.html#213045577">(Oct 12 2020 at 14:31)</a>:</h4>
<p>Note that an AST validation pass is not enough to handle this – the validation needs to happen on expansion of the built-in <code>format_args!</code> macro</p>



<a name="213045771"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Format%20string%20syntax%20%233419/near/213045771" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Format.20string.20syntax.20.233419.html#213045771">(Oct 12 2020 at 14:32)</a>:</h4>
<p><span class="user-mention" data-user-id="300586">@veykril</span> right! I think it might make sense to move it to a <code>format.rs</code> module, and maybe change the interface to work with <code>&amp;str</code>, rather than with AST nodes</p>



<a name="213045793"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Format%20string%20syntax%20%233419/near/213045793" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Format.20string.20syntax.20.233419.html#213045793">(Oct 12 2020 at 14:32)</a>:</h4>
<p>Things that can be indepemdent of AST should be independent</p>



<a name="213045863"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Format%20string%20syntax%20%233419/near/213045863" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Format.20string.20syntax.20.233419.html#213045863">(Oct 12 2020 at 14:33)</a>:</h4>
<p>It is also true that it's only  during macro expansion that we know which strings are actually format specifiers.</p>



<a name="213046097"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Format%20string%20syntax%20%233419/near/213046097" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lukas Wirth <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Format.20string.20syntax.20.233419.html#213046097">(Oct 12 2020 at 14:34)</a>:</h4>
<p>Right, where should the check go then though? I've been looking through things for a bit earlier but couldn't find another place where it was obvious for me to check.</p>



<a name="213046172"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Format%20string%20syntax%20%233419/near/213046172" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lukas Wirth <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Format.20string.20syntax.20.233419.html#213046172">(Oct 12 2020 at 14:35)</a>:</h4>
<p>What crate should that belong to then? <code>stdx</code> or still <code>syntax</code>?</p>



<a name="213046461"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Format%20string%20syntax%20%233419/near/213046461" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Format.20string.20syntax.20.233419.html#213046461">(Oct 12 2020 at 14:37)</a>:</h4>
<p><code>format_args_expand</code> I think is conceptually the right place to do the check</p>



<a name="213046519"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Format%20string%20syntax%20%233419/near/213046519" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Format.20string.20syntax.20.233419.html#213046519">(Oct 12 2020 at 14:37)</a>:</h4>
<p>The problem is, we don't yet have any infra for reporting errors from macro expansion so this'll require some plumbing</p>



<a name="213047817"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Format%20string%20syntax%20%233419/near/213047817" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lukas Wirth <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Format.20string.20syntax.20.233419.html#213047817">(Oct 12 2020 at 14:48)</a>:</h4>
<p>Oh, so invalid macro invocations aren't actually being reported at all by RA currently?</p>



<a name="213049700"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Format%20string%20syntax%20%233419/near/213049700" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Format.20string.20syntax.20.233419.html#213049700">(Oct 12 2020 at 15:02)</a>:</h4>
<p>yup</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>